﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Windows.Resources;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using PivotApp1;

namespace PhoneApp1
{
    public partial class Page1 : PhoneApplicationPage
    {
        private SoundEffect C;
        private SoundEffect D;
        private SoundEffect G;
        private SoundEffect A;
        private SoundEffect CD;
        private SoundEffect CG;
        private SoundEffect CA;
        private SoundEffect DG;
        private SoundEffect DA;
        private SoundEffect GA;
        private SoundEffect CDG;
        private SoundEffect CDA;
        private SoundEffect CGA;
        private SoundEffect DGA;
        private SoundEffect CDGA;
        private static GameTimer player;
       // private bool[,] MainPage.chordsmatrix;
        private int timercolumn;
        private List<rectangle_Control> rectangles;
        private static double bpm = 250;

       // private static int numrows = 4;
      //  private static int numcolumns = 15;


        // Constructor
        public Page1()
        {
            InitializeComponent();

            GameTimer gameTimer = new GameTimer();
            gameTimer.UpdateInterval = TimeSpan.FromMilliseconds(20);

            // Call FrameworkDispatcher.Update to update the XNA Framework internals.
            gameTimer.Update += delegate { try { FrameworkDispatcher.Update(); } catch { } };

            // Start the GameTimer running.
            gameTimer.Start();

            player = new GameTimer();
            player.UpdateInterval = TimeSpan.FromMilliseconds(bpm);
            player.Update += delegate { playSound(); };
            player.Start();

            rectangles = new List<rectangle_Control>();

            // Prime the pump or we'll get an exception.
            FrameworkDispatcher.Update();
            timercolumn = 0;
            //MainPage.chordsmatrix = new bool[numcolumns, numrows];
            
            LoadSound("Resources/10 A.wav", out A);
            LoadSound("Resources/14 CDGA.wav", out CDGA);
            LoadSound("Resources/1 C.wav", out C);
            LoadSound("Resources/1 CD.wav", out CD);
            LoadSound("Resources/11 CDA.wav", out CDA);
            LoadSound("Resources/12 CGA.wav", out CGA);
            LoadSound("Resources/13 DGA.wav", out DGA);
            LoadSound("Resources/2 CG.wav", out CG);
            LoadSound("Resources/3 D.wav", out D);
            LoadSound("Resources/8 DA.wav", out DA);
            LoadSound("Resources/8 G.wav", out G);
            LoadSound("Resources/10 CDG.wav", out CDG);
            LoadSound("Resources/6 GA.wav", out GA);
            LoadSound("Resources/3 CA.wav", out CA);
            LoadSound("Resources/7 DG.wav", out DG);

            int i = 0;
            int j = 0;


            while (i < MainPage.numrows)
            {
                RowDefinition a = new RowDefinition();
                a.Height = new GridLength(1, GridUnitType.Star);
                // a.Height = GridLength.Auto;
                ContentPanel.RowDefinitions.Add(a);
                i++;
            }
            i = 0;
            while (i < MainPage.numcolumns)
            {
                ColumnDefinition b = new ColumnDefinition();
                b.Width = new GridLength(1, GridUnitType.Star);
                ContentPanel.ColumnDefinitions.Add(b);
                i++;
            }
            i = 0;
            while (i < MainPage.numrows)
            {

                while (j < MainPage.numcolumns)
                {
                    rectangle_Control r = new rectangle_Control();
                    r.SetValue(Grid.RowProperty, i);
                    r.SetValue(Grid.ColumnProperty, j);
                    r.MouseLeftButtonDown += new MouseButtonEventHandler(rectangle_Control_MouseLeftButtonDown);
                    r.Name = "t" + j.ToString() + i.ToString();
                    r.Margin = new Thickness(5);
                    ContentPanel.Children.Add(r);
                    if (MainPage.chordsmatrix[j, i])
                    {
                        r.hilight_Control.Begin();
                        rectangles.Add(r);
                    }
                    j++;
                }
                j = 0;
                i++;
            }
            ContentPanel.UpdateLayout();
          

        }

        public static void stopTimer()
        {
            if (player != null)
            {
                player.Stop();
            }
        }

        public static void BPM(double value)
        {
            bpm = value;
        }




        private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        {
            // For error checking, assume we'll fail to load the file.
            Sound = null;

            try
            {
                // Holds informations about a file stream.
                StreamResourceInfo SoundFileInfo =  App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

                // Create the SoundEffect from the Stream
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);

            }
            catch (NullReferenceException)
            {
                // Display an error message
                MessageBox.Show("Couldn't load sound " + SoundFilePath);
            }
        }





        private void playSound()
        {


            foreach (rectangle_Control r in rectangles)
            {
                int p = int.Parse(r.GetValue(Grid.ColumnProperty).ToString());
                if (p == timercolumn)
                {
                    r.blinker();
                    
                }
                
            }

            if (!MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                C.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                D.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CD.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                G.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CG.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                DG.Play();
            }
            if (!MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CDG.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                A.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                DA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && !MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CDA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                GA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && !MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CGA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && !MainPage.chordsmatrix[timercolumn, 3])
            {
                DGA.Play();
            }
            if (MainPage.chordsmatrix[timercolumn, 0] && MainPage.chordsmatrix[timercolumn, 1] && MainPage.chordsmatrix[timercolumn, 2] && MainPage.chordsmatrix[timercolumn, 3])
            {
                CDGA.Play();
            }

            if (timercolumn <= MainPage.numcolumns-2)
            {
                timercolumn++;
            }
            else
            {
                timercolumn = 0;
            }
        }

        private void startPlayer()
        {
            player.Start();
        }

        private void rectangle_Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            rectangle_Control a = sender as rectangle_Control;


            if (rectangles.Contains(a))
            {
                rectangles.Remove(a);
                a.normal();
                string name = a.Name;
                int col = int.Parse(a.GetValue(Grid.ColumnProperty).ToString());
                int row = int.Parse(a.GetValue(Grid.RowProperty).ToString());
               
              //  MainPage.chordsmatrix[col, row] = false;
                MainPage.chordsmatrix[col, row] = false;
            }
            else
            {
                rectangles.Add(a);
                string name = a.Name;
                int col = int.Parse(a.GetValue(Grid.ColumnProperty).ToString());
                int row = int.Parse(a.GetValue(Grid.RowProperty).ToString());
               // MainPage.chordsmatrix[col, row] = true;
                MainPage.chordsmatrix[col, row] = true;
                
            }
        }
    }
}